package com.bbs.controller;

import com.bbs.anno.MyLog;
import com.bbs.dto.CommentDTO;
import com.bbs.dto.Result;
import com.bbs.service.CommentService;
import com.bbs.vo.CommentVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/comments")
@Tag(name = "评论管理", description = "评论相关的接口")
public class CommentController {
    
    @Autowired
    private CommentService commentService;

    @MyLog
    @PostMapping
    @Operation(summary = "创建评论", description = "创建新评论")
    public Result createComment(@RequestBody CommentDTO commentDTO) {
        CommentVO commentVO = commentService.createComment(commentDTO);
        return Result.ok(commentVO);
    }
    
    @GetMapping("/{id}")
    @Operation(summary = "获取评论信息", description = "根据评论ID获取评论信息")
    @Parameter(name = "id", description = "评论ID", required = true)
    public Result getCommentById(@PathVariable Long id) {
        CommentVO commentVO = commentService.getCommentById(id);
        return Result.ok(commentVO);
    }
    
    @GetMapping
    @Operation(summary = "获取所有评论", description = "获取系统中所有评论")
    public Result getAllComments() {
        return Result.ok(commentService.getAllComments());
    }
    
    @PutMapping("/{id}")
    @Operation(summary = "更新评论", description = "根据评论ID更新评论信息")
    @Parameter(name = "id", description = "评论ID", required = true)
    public Result updateComment(@PathVariable Long id, @RequestBody CommentDTO commentDTO) {
        CommentVO commentVO = commentService.updateComment(id, commentDTO);
        return Result.ok(commentVO);
    }
    
    @DeleteMapping("/{id}")
    @Operation(summary = "删除评论", description = "根据评论ID删除评论")
    @Parameter(name = "id", description = "评论ID", required = true)
    public Result deleteComment(@PathVariable Long id) {
        commentService.deleteComment(id);
        return Result.ok();
    }
    
    @GetMapping("/post/{postId}")
    @Operation(summary = "获取帖子评论", description = "获取指定帖子的所有评论")
    @Parameter(name = "postId", description = "帖子ID", required = true)
    public Result getCommentsByPostId(@PathVariable Long postId) {
        return Result.ok(commentService.getCommentsByPostId(postId));
    }
    
    @GetMapping("/user/{userId}")
    @Operation(summary = "获取用户评论", description = "获取指定用户的所有评论")
    @Parameter(name = "userId", description = "用户ID", required = true)
    public Result getCommentsByUserId(@PathVariable Long userId) {
        return Result.ok(commentService.getCommentsByUserId(userId));
    }
} 